package com.macrosoft.frpd.bo;

/**
 * @author aizaz.haider 
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public final class FeedItem {

	// ===========================================================
	// Constants
	// ===========================================================
	private final String TITLE;
	private final String FEED_URL;
	private List<FeedItem> mChildren;
	private HashMap<String, String> mProperties;

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================
	public FeedItem(String pTitle, String pURL) {
		this.TITLE = pTitle;
		this.FEED_URL = pURL;
	}

	public FeedItem(String pTitle, String pURL, ArrayList<FeedItem> pChildren) {
		this.TITLE = pTitle;
		this.FEED_URL = pURL;

		this.initChildren(0);
		Collections.copy(mChildren, pChildren);
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public String getTitle() {
		return TITLE;
	}

	public String getURL() {
		return FEED_URL;
	}

	public List<FeedItem> getChildren() {
		return Collections.unmodifiableList(mChildren);
	}

	public HashMap<String, String> getProperties() {
		return (this.mProperties);
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================
	public boolean hasChildren() {
		return mChildren == null ? false : mChildren.size() > 0 ? true : false;
	}

	public boolean addChild(FeedItem pItem) {
		this.initChildren(0);

		return mChildren.add(pItem);
	}

	public boolean addChild(FeedItem pItem, int numbOfChildren) {
		this.initChildren(numbOfChildren);

		return mChildren.add(pItem);
	}

	public String addProperty(String pkey, String pValue) {
		this.initProperties();

		return this.mProperties.put(pkey, pValue);
	}

	/**
	 * If list is <b>null</b> then initialize it.
	 */
	private void initChildren(int size) {
		if (mChildren == null && size > 0) {
			mChildren = new ArrayList<FeedItem>(size);
		} else if (mChildren == null) {
			mChildren = new ArrayList<FeedItem>();
		}

	}

	private void initProperties() {
		if (mProperties == null)
			mProperties = new HashMap<String, String>();
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}